package gmail.pyanoveugen.studyproject.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 * Created with IntelliJ IDEA.
 * User: Ievgen_Pianov
 * Date: 9/21/12
 * Time: 10:58 AM
 * To change this template use File | Settings | File Templates.
 */
public class DaoFactory {


    private static DaoFactory instance;
    public static final String DRIVER_NAME = "org.hsqldb.jdbcDriver";
    private Connection connection;
    private static final String URL = "jdbc:hsqldb:hsql://localhost/";
    private static final String USER_NAME = "sa";
    private static final String PASSWORD = "";

    public static DaoFactory getInstance(){
        if(instance == null){
            instance = new DaoFactory();
        }

        return instance;
    }

    private DaoFactory() {
        // load driver

        try {
            Class.forName(DRIVER_NAME);
        } catch (ClassNotFoundException e) {
            //TODO Log me
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            throw new RuntimeException(e);
        }


        // connect to DB
        try {
            connection = DriverManager.getConnection(URL, USER_NAME, PASSWORD);
        } catch (SQLException e) {
            //TODO log me
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            throw new RuntimeException(e);
        }

    }

    public UserDao getUserDao(){
        return new UserDao(connection);
    }

}
